<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="box-card">
      <!-- 添加权限列表 -->
      <el-table :data="authList" border stripe style="width: 100%">
        <el-table-column type="index" label="#">
        </el-table-column>
        <el-table-column prop="name" label="权限名称" width="180">
        </el-table-column>
        <el-table-column prop="url" label="权限路径" width="180">
        </el-table-column>
        <el-table-column prop="level" label="权限级别" width="180">
          <template v-slot="scope">
            <el-tag type="success" v-if="scope.row.level === 1">一级</el-tag>
            <el-tag type="warning" v-else-if="scope.row.level === 2">二级</el-tag>
            <el-tag type="danger"  v-else-if="scope.row.level === 3">三级</el-tag>
          </template>
        </el-table-column>
        <el-table-column  label="权限所属" width="180">
          <template v-slot="scope">
            {{showParent(scope.row.pid)}}
          </template>
        </el-table-column>
      </el-table>

    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 权限列表
      authList: []
    }
  },
  created () {
    this.getAuthList()
  },
  methods: {
    // 获取列表权限
    async getAuthList () {
      const {
        data: res
      } = await this.$http.get('auth/auth/list', {
        params: {
          type: 'list'
        }
      })
      if (res.result !== 'SUCCESS') {
        return this.$message.error(res.message)
      }
      this.$message.success('列表获取成功')
      this.authList = res.data.authList
    },
    // 显示父节点的名称
    showParent (pid) {
      if (pid === null || pid === '') {
        return ''
      }
      for (const auth of this.authList) {
        if (auth.id === pid) {
          return auth.name
        }
      }
      return ''
    }
  }
}
</script>

<style>
  .el-card {
    margin-top: 20px;
  }
</style>
